package com.cbgolf.oa.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.TeeTimeBean.TeeTimesBean;
import com.cbgolf.oa.util.CollectionUtil;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;
import java.util.ArrayList;
import java.util.List;
import org.xutils.view.annotation.ViewInject;

/**
 * @author lq
 * @date 2017/12/11
 */

public class TeeTimeAdapter extends RecyclerView.Adapter<TeeTimeAdapter.Holder> {

  private List<TeeTimesBean> list;
  private Context context;
  private LayoutInflater mInflater;
  private ITeetimeAdapterWaiter worker;

  public interface ITeetimeAdapterWaiter {

    void addPerson(TeeTimesBean bean);

    void reducePerson(TeeTimesBean bean);
  }

  public TeeTimeAdapter(Context context, ITeetimeAdapterWaiter worker) {
    this.context = context;
    this.worker = worker;
    list = new ArrayList<>();
    mInflater = LayoutInflater.from(context);
  }

  public void clear() {
    CollectionUtil.clear(list);
  }

  public void setData(List<TeeTimesBean> list) {
    this.list = list;
  }

  @Override
  public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.item_teetime, null);
    return new Holder(view);
  }


  @Override
  public void onBindViewHolder(Holder holder, int position) {
    if (position % 2 == 0) {
      holder.left5.setVisibility(View.VISIBLE);
      holder.left15.setVisibility(View.VISIBLE);
      holder.right5.setVisibility(View.VISIBLE);
      holder.right15.setVisibility(View.GONE);
    } else {
      holder.left5.setVisibility(View.VISIBLE);
      holder.left15.setVisibility(View.GONE);
      holder.right5.setVisibility(View.VISIBLE);
      holder.right15.setVisibility(View.VISIBLE);
    }
    ViewUtils.setVisible(holder.bottom15, position == getItemCount() - 1);
    holder.priceView.setVisibility(View.INVISIBLE);
    final TeeTimesBean m = list.get(position);
    if (null == m) {
      return;
    }
    holder.timeTv.setText(DateUtil.stampToDateHm(m.teeTimeDate));
    ImageView[] ivs = new ImageView[]{
        holder.iv1, holder.iv2, holder.iv3, holder.iv4
    };
    View[] checkViews = new View[]{
        holder.checkView1, holder.checkView2, holder.checkView3, holder.checkView4
    };
    for (int i = 0; i < checkViews.length; i++) {
      checkViews[i].setTag(position);
      ivs[i].setTag(position);
      checkViews[i].setEnabled(true);
    }
    for (int ivIndex = 0; ivIndex < ivs.length; ivIndex++) {
      if (Util.listIsNull(m.sourceTypeLists)) {
        ivs[ivIndex].setImageResource(R.drawable.stroke_blue_round_25);
        continue;
      }
      if (position == (int) checkViews[ivIndex].getTag()) {
        if (ivIndex < m.sourceTypeLists.size()) {
          ivs[ivIndex]
              .setImageResource(EnumUtil.isFromApp(m.sourceTypeLists.get(ivIndex)) ?
                  R.drawable.oval_blue
                  : R.mipmap.yuding_choice);
          checkViews[ivIndex].setEnabled(!EnumUtil.isFromApp(m.sourceTypeLists.get(ivIndex)));
        } else {
          ivs[ivIndex].setImageResource(R.drawable.stroke_blue_round_25);
          checkViews[ivIndex].setEnabled(true);
        }
      }
    }
    for (int i = 0; i < checkViews.length; i++) {
      int finalI = i;
      ViewUtils.setClick(checkViews[i], v -> {
        if (!DataUtil.isHasTeetimePermission() || null == worker) {
          return;
        }
        if (Util.listIsNull(m.sourceTypeLists) || m.sourceTypeLists.size() <= finalI) {
          worker.addPerson(m);
          return;
        }
        if (m.sourceTypeLists.size() > finalI
            && !EnumUtil.isFromApp(m.sourceTypeLists.get(finalI))) {
          worker.reducePerson(m);
        }
      });
    }
  }


  @Override
  public int getItemCount() {
    return Util.listIsNull(list) ? 0 : list.size();
  }

  public class Holder extends RecyclerView.ViewHolder {

    @ViewInject(R.id.item_teetime_layout_ll)
    private View item;
    @ViewInject(R.id.item_teetime_time_tv)
    private TextView timeTv;
    @ViewInject(R.id.item_teetime_price_ll)
    private View priceView;
    //
    @ViewInject(R.id.check1)
    private View checkView1;
    @ViewInject(R.id.check2)
    private View checkView2;
    @ViewInject(R.id.check3)
    private View checkView3;
    @ViewInject(R.id.check4)
    private View checkView4;
    @ViewInject(R.id.item_teetime_iv2)
    private ImageView iv2;
    @ViewInject(R.id.item_teetime_iv3)
    private ImageView iv3;
    @ViewInject(R.id.item_teetime_iv1)
    private ImageView iv1;
    @ViewInject(R.id.item_teetime_iv4)
    private ImageView iv4;

    @ViewInject(R.id.left15)
    private View left15;
    @ViewInject(R.id.left5)
    private View left5;
    @ViewInject(R.id.right15)
    private View right15;
    @ViewInject(R.id.right5)
    private View right5;
    @ViewInject(R.id.top10)
    private View top10;
    @ViewInject(R.id.bottom15)
    private View bottom15;

    public Holder(View view) {
      super(view);
      ViewUtils.autoInjectAllField(this, view);
      AutoUtil.autoSize(view);
    }
  }
}
